############################################
#Project options
TARGET =	libcudaaes.a
NOCUDA=0

CU_SOURCES =	CudaAES.cu cudaUtils.cu
CU_DEPS = CudaAESOperations.cu CudaAESInvOperations.cu CudaAESFast.cu
CU_OBJECTS = 	$(CU_SOURCES:.cu=.o)

OBJECTS = aesUtils.o Encrypter.o Decrypter.o BigInteger.o\
		 AesSwitchEncrypter.o OpenSSLAESEncrypter.o OpenSSLAESDecrypter.o 

ifneq (${NOCUDA}, 1)		 
OBJECTS+= CUDAAESEncrypter.o CUDAAESDecrypter.o $(CU_OBJECTS)
endif 

TESTS = 
ifneq (${NOCUDA}, 1)
TESTS +=EncDecrypterTests
# CudaRijndaelTests  BigIntegerTests
# AesKeyTest CudaAesOperationsTest CudaAesInvOperationsTest 
endif

TEST_DEPS = ../obj/$(TARGET)

LIBS = -lcuda -lcudart -lssl
TEST_LIBS = -lcppunit -lssl -lcudaaes

INCLUDE = /opt/cuda/include ./
LIBDIR = -L/opt/cuda/lib64 -L./obj/

OBJDIR = obj
SRCDIR = src
TSTDIR = test
TARGET := $(addprefix $(OBJDIR)/, $(TARGET))
############################################
#Compilation options


CXX = g++
CC = gcc
NVCC = nvcc
CXXFLAGS =	-O2 -g
# -Wall -fmessage-length=0
NVCCFLAGS = -O2 -g 
LDFLAGS=

INCLUDE := $(addprefix -I, $(INCLUDE))
NDEBUG=1
ifdef NDEBUG
CXXFLAGS := -DNDEBUG -O2 -Wall
NVCCFLAGS := -DNDEBUG -O2
else
CXXFLAGS := -g 
# -deviceemu
#-Wall
NVCCFLAGS := -g 
#-deviceemu
endif

ifeq (${NOCUDA}, 1)
CXXFLAGS+=-DNOCUDA
CFLAGS+=-DNOCUDA
endif

############################################
#Build rules

all:  $(TARGET) UNIT_TESTS



# $^ skladniki reguly
# $@ regula
# regula: skladniki
$(TARGET): $(addprefix $(OBJDIR)/, $(OBJECTS)) 
	ar rcs $@ $^
#	$(CXX) $(LIBDIR) $(LDFLAGS) $^ $(LIBS) -o $@



$(OBJDIR)/%.o : $(SRCDIR)/%.cpp
		$(CXX) $(CXXFLAGS) $(INCLUDE) $< -c -o $@


$(OBJDIR)/%.o : $(SRCDIR)/%.cu $(addprefix $(SRCDIR)/, $(CU_DEPS))
		$(NVCC) $(NVCCFLAGS) $(INCLUDE) $< -c -o $@


UNIT_TESTS: $(addprefix $(TSTDIR)/, $(TESTS))
		@cat $(TSTDIR)/runAll.sh | sort | uniq > $(TSTDIR)/runAll2.sh && mv $(TSTDIR)/runAll2.sh $(TSTDIR)/runAll.sh
		@sh $(TSTDIR)/runAll.sh


$(TSTDIR)/% : $(TSTDIR)/%.cpp $(TARGET)
		$(CXX) $(CXXFLAGS) $(INCLUDE) $(LIBDIR) $< $(TEST_LIBS) $(LIBS) -o  $@
		@echo $@ >> $(TSTDIR)/runAll.sh

			
clean:
	rm -f $(addprefix $(OBJDIR)/, $(OBJECTS)) $(TARGET)  $(addprefix $(TSTDIR)/, $(TESTS)) test/runAll.sh
	touch test/runAll.sh
